var menudata = {};
var app = getApp();
var curLeftIndex;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    leftList: [],
    rightList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 搜索栏
  onLoad: function () {
    curLeftIndex = 0;
    app.ready(() => {
      this.loadMenusData();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },

  wxSearchTab: function () {
    wx.navigateTo({
      url: './search/search?target=goods',
    });
  },
  changeCategory: function (event) {
    var mtype = event.currentTarget.dataset.mtype;
    curLeftIndex = event.currentTarget.dataset.index;
    if(menudata[mtype].length > 0){
      var rightList = menudata[mtype];
      this.setData({
        rightList: rightList,
        scrollTop: 0,
        curType: mtype
      });
    }
    else{
      this.loadSubMenu(this.data.leftList[curLeftIndex].id);
    }
  },
  goodsType_tap: function (event) {
    var goodstype = event.currentTarget.dataset.goodstype;
    wx.navigateTo({
      url: './goodslist/goodslist?goodstype=' + goodstype,
    })
  },
  loadMenusData: function () {
    var url = "/index.php/api/Goods/categoryList";
    app.request(url,{}).then(this.reqCallBackFuc1.bind(this));
  },
  reqCallBackFuc1: function (result) {
    if (result == 'err') {
      return;
    }
    var dataArr = result.result;
    var leftList = [];
    for (var i = 0; i < dataArr.length; i++) {
      var menu = {};
      menu.id = dataArr[i].id;
      menu.mtype = 'm' + dataArr[i].id;
      menu.name = dataArr[i].mobile_name;
      menudata['m' + dataArr[i].id] = [];
      leftList.push(menu);
    }
    // menudata['m2'] = [];
    // leftList.push({name:'运动护理',mtype:'m2'});
    this.setData({
      leftList: leftList
    });
    this.loadSubMenu(leftList[curLeftIndex].id)
  },
  loadSubMenu: function (catid) {
    app.showLoading();
    var url = '/index.php/api/Goods/twocategoryList';
    var params = {
      catid: catid
    }
    app.request(url, params).then(this.reqCallBackFuc2.bind(this));
  },
  reqCallBackFuc2: function (result) {
    app.hideLoading();
    var cateArr = [];
    for (var i = 0; i < result.result.length; i++) {
      var cateinfo = {};
      cateinfo.name = result.result[i].mobile_name;
      var img = result.result[i].image
      var contains = [];
      for (var j = 0; j < result.result[i].sub_menu.length; j++) {
        var third_menu = {};
        third_menu.name = result.result[i].sub_menu[j].mobile_name;
        third_menu.image = result.result[i].sub_menu[j].image;
        if (!third_menu.image) {
          third_menu.image = '../imgs/tempic/goods6.jpg';
        }
        else{
          third_menu.image = app.globalData.webUrl + third_menu.image;
        }
        third_menu.activetype = result.result[i].sub_menu[j].id;
        contains.push(third_menu);
      }
      cateinfo.contains = contains;
      cateArr.push(cateinfo);
    }
    var parentid = result.result[0].parent_id;
    menudata['m' + parentid] = cateArr;
    var keystr = this.data['leftList'][curLeftIndex]['mtype'];
    var rightList = menudata[keystr];
    this.setData({
      rightList: rightList,
      scrollTop: 0,
      curType: keystr
    });
  },
  toCouponCenter:function(){
    wx.navigateTo({
      url: '../my/coupon/getcoupon',
    });
  }
})